package BankAccount;

import java.util.ArrayList;

/**
 * @author Zhiye Huang
 * @version 1.0
 * <p>
 * 7 steps to write an OO program
 * <p>
 * 1. Convert UML to Java code.
 * (Actually a class diagram notation, which is part of a widely used software design notation, UML.)
 * <p>
 * The general template:
 * ClassName {
 * instance variables
 * constructors
 * accessors (or getters)
 * mutators (or setters)
 * service method
 * }
 * <p>
 * 2. instance variables (Any changes to the obj's state should be made by obj's method)
 * 3. constructors
 * 4. accessors (or getters) and mutators (or setters)
 * 5. service method
 * 6. toString()
 * 7. test class
 */

public class BankAccount {
    private String accNumber = "Invalid";
    private String accName = "Invalid";
    private double balance = 0.0;
    private ArrayList<String> message = new ArrayList<String>();

    public BankAccount() {

    }

    public BankAccount(String accNumber, String accName) {
        setAccNumber(accNumber);
        setAccName(accName);
    }

//    public BankAccount(String accName, String accNumber) DameDane!

    public BankAccount(String accNumber, String accName, double balance) {
        setAccNumber(accNumber);
        setAccName(accName);
        setBalance(balance);
    }

    public String getAccNumber() {
        return this.accNumber;
    }

    public String getAccName() {
        return this.accName;
    }

    public double getBalance() {
        return this.balance;
    }

    public void setAccNumber(String accNumber) {
        this.accNumber = accNumber;
    }

    public void setAccName(String accName) {
        this.accName = accName;
    }

    public void setBalance(double balance) {
//        if (balance >= 0.0) this.balance = balance;
//        else System.out.printf("Invalid balance %f\n", balance);
        this.balance = balance;
    }

    public void receiveMessage(String message) {
        this.message.add(message);
    }

    public void ShowMessages() {
        StringBuilder sbr = new StringBuilder();
        for (int i = 0; i < this.message.size(); i++) {
            sbr.append("Message " + i + ":" + '\n');
            sbr.append(this.message.get(i) + '\n');
        }
        System.out.println(sbr.toString());
    }

    public void deposit(double amount) {
        this.balance += amount;
//        System.out.println(getBalance());
    }

    public void withDraw(double amount) {
//        if (amount < 0.0) System.out.printf("Invalid amount %.2f\n", amount);
//        else{
//            double left = this.balance - amount;
//            if (left >= 0.0) this.balance = left;
//            else if (-left <= limit) this.balance = left;
//            else System.out.printf("Balance: %.2f $\nWARNING! You are in over debt! Withdraw operation failed.\n", this.balance);
//        }
        this.balance -= amount;
    }

    public boolean equals(Object o) {
        if (o == null) return false;
        if (!o.getClass().equals(this.getClass())) return false;
        BankAccount b = (BankAccount) o;
        return this.accNumber.equals(b.accNumber) && this.accName.equals(b.accName);
    }

    public boolean equals(String accNumber, String accName) {
        return this.accNumber.equals(accNumber) && this.accName.equals(accName);
    }

    /**
     * A beautiful print method of BankAccount class.
     * @return
     */
    public String toString() {
        String str = "";
        String filler = " ";
        int frameLen = 65;
        int frameWidth = 10;

        for (int i = 1; i <= frameWidth; i++) {
            String lineText = "";
            filler = " ";
            if (i == 1 || i == frameWidth) {
                for (int j = 0; j < frameLen; j++) {
                    lineText += "=";
                }
            } else {
                lineText += "||";
                String content = "";
                int InsertPos;
                if (i == 2) content = "JMRCB";
                if (i == 3) {
                    filler = "-";
                }
                if (i == 4) {
                    content = "ACCOUNT NUMBER:";
                    int limit = content.length() + String.valueOf(getAccNumber()).length();
                    for (int j = limit; j < frameLen / 2; j++) {
                        content += filler;
                    }
                    content += getAccNumber();
                }
                if (i == 6) {
                    content = "ACCOUNT NAME:";
                    int limit = content.length() + String.valueOf(getAccName()).length();
                    for (int j = limit; j < frameLen / 2; j++) {
                        content += filler;
                    }
                    content += getAccName();
                }
                if (i == 8) {
                    content = "ACCOUNT BALANCE:";
                    int limit = content.length() + + String.format("%.2f", getBalance()).length() + 2;
                    for (int j = limit; j < frameLen / 2; j++) {
                        content += filler;
                    }
                    content += String.format("%.2f$", getBalance());
                }

                InsertPos = ((frameLen - content.length()) / 2);
                for (int j = 1; j < InsertPos; j++) {
                    lineText += filler;
                }
                lineText += content;
                for (int j = lineText.length(); j < frameLen - 2; j++) {
                    lineText += filler;
                }
                lineText += "||";

            }
            lineText += '\n';
            str += lineText;
        }

        return str;
    }
}
